package polyu.comp;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;



public class RecentActivity extends ListActivity {
	
	//ArrayAdapter<String> adapter;
	private LinkedList<String> mListItems;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recent_layout);

        // Set a listener to be invoked when the list should be refreshed.
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
            	new GetDataTask().execute();
            }
            
        });
        
        mListItems = new LinkedList<String>();
        Vector<ActionItem> vi = DataManager.instance().getItems();
        for (ActionItem i : vi)
        	mListItems.add(i.toString());


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        		this,
                android.R.layout.simple_list_item_1, 
                mListItems);

        setListAdapter(adapter);
        
    }
    
    public class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                ;
            }
            
            boolean suc = DataManager.instance().pullFromServer();
            
            if (false == suc) {
            	return null;
            }
        	
            Vector<ActionItem> vi = DataManager.instance().getItems();
            ArrayList<String> al = new ArrayList<String>();
            for (ActionItem i : vi)
            	al.add(i.toString());
       
            return al.toArray(new String[0]);
        }

        @Override
        protected void onPostExecute(String[] result) {
        	
        	if (result == null) {
        		Toast.makeText(getApplicationContext(), "You're offline. Cached messages are displayed", Toast.LENGTH_LONG).show();
        	} 
        	else {
        		 mListItems.clear();
                 mListItems.addAll(Arrays.asList(result));
                 String msg = String.format("%d new items retrieved", mListItems.size());
                 Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
        	}

            // Call onRefreshComplete when the list has been refreshed.
            ((PullToRefreshListView) getListView()).onRefreshComplete();

            super.onPostExecute(result);
        }
    }
    
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
    	//Vector<ActionItem> vi = DataManager.instance().getItems();
    	
    	//Toast.makeText(this, vi.get(position-1).toString(), Toast.LENGTH_LONG).show();
    	
    	
    	//Util.fTest(this);
    	
    	Intent it = new Intent();
		it.setClass(this, MapRouteActivity.class);
		startActivity(it);
		
		
	}
    
}
